﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;

using SDLITS.Common;
using SDLITS.Model;
using MySoft.Data;

namespace SDLITS.Web.SystemMgr
{
    public partial class SysModelMgrChange : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropType();
                txt_Icon.OnClientTriggerClick = Window1.GetSaveStateReference(txt_Icon.ClientID)
                         + Window1.GetShowReference("../SystemManage/SysSelIcon.aspx", "选择图标");
                LoadData();

            }

        }

        private void DropType()
        {
            List<Menu> items = SDDbSession.Default.From<Menu>().Where(Menu._.ParentID == 0).OrderBy(Menu._.MenuID.Asc).ToList();
            foreach (Menu row in items)
            {
                int count = SDDbSession.Default.From<Menu>().Where(Menu._.ParentID == row.MenuID).OrderBy(Menu._.MenuID.Asc).Count();
               FineUI. ListItem li = new FineUI. ListItem();
                li.Text = row.MenuName;
                li.Value = row.MenuID.ToString();
                txt_topmid.Items.Add(li);
                if (count > 0)
                {
                    ResolveSubTree(row.MenuID, 0);
                }
            }
            FineUI.ListItem li2 = new FineUI.ListItem();
            li2.Text = "--顶级模块--";
            li2.Value = "0";
            txt_topmid.Items.Insert(0, li2);
            if (!string.IsNullOrEmpty(Request["pid"]))
            {
                txt_topmid.SelectedValue = Request["pid"];
            }
        }

        private void ResolveSubTree(int id, int Leve)
        {
            Leve++;
            List<Menu> items = SDDbSession.Default.From<Menu>().Where(Menu._.ParentID == id).OrderBy(Menu._.MenuID.Asc).ToList();
            if (items.Count > 0)
            {
                foreach (Menu row in items)
                {
                    int count =SDDbSession.Default.From<Menu>().Where(Menu._.ParentID == row.MenuID).OrderBy(Menu._.MenuID.Asc).Count();
                    FineUI.ListItem li = new FineUI.ListItem();
                    li.SimulateTreeLevel = Leve;
                    li.Text = row.MenuName;
                    li.Value = row.MenuID.ToString();
                    txt_topmid.Items.Add(li);
                    if (count > 0)
                    {
                        ResolveSubTree(row.MenuID, Leve);
                    }
                }
            }

        }

        private void LoadData()
        {

            // 关闭按钮的客户端脚本：
            // 1. 首先确认窗体中表单是否被修改（如果已经被修改，则弹出是否关闭的确认对话框）
            // 2. 然后关闭本窗体，回发父窗体
            btnClose.OnClientClick =FineUI. ActiveWindow.GetConfirmHidePostBackReference();
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                Menu tm = SDDbSession.Default.From<Menu>().Where(Menu._.MenuID == int.Parse(Request["id"])).ToFirst();
                txt_FunctionPage.Text = tm.WebUrl;
                txt_isuser.Checked = tm.IsShow.Value;
              //  txt_keyword.Text = tm.MenuNode;
              //  txt_memo.Text = tm.Memo;
                txt_ModeuleName.Text = tm.MenuName;
                txt_topmid.SelectedValue = tm.ParentID.Value.ToString();
              //  txt_orderid.Text = tm.Orderid.Value.ToString();
                txt_Icon.Text = tm.IconUrl;
            }
            #region old code
            // 1.第一种调用方法
            //string confirmText = "是否确认关闭当前页？<br/>您在当前页所做的修改没有保存。继续编辑当前页，请选择“取消”。<br/>选择“确定”关闭当前页，选择“取消”继续编辑当前页。";
            //string closeScript = CurrentActiveWindow.GetCloseReference();
            //string confirmScript = ChanExt.Confirm.GetShowReference(confirmText, "确认关闭", ChanExt.MessageBoxIcon.Warning, closeScript, "return false;");
            //btnClose.OnClientClick = String.Format("if({0}){{{1}}}else{{{2}}}", ChanExt.PageContext.GetPageStateChangedReference(), confirmScript, closeScript);

            //// 2.第二种调用方法
            //string confirmText = "是否确认关闭当前页？<br/>您在当前页所做的修改没有保存。继续编辑当前页，请选择“取消”。<br/>选择“确定”关闭当前页，选择“取消”继续编辑当前页。";
            //string closeScript = CurrentActiveWindow.GetCloseReference();
            //btnClose.OnClientClick = ChanExt.PageContext.GetPageStateChangedConfirmReference("确认关闭", confirmText, closeScript, "return false;");

            //// 3.第三种调用方法（并且在父页面中可以简单的 window1.OnClientCloseButtonClick = window1.GetIFramePageStateChangedFunction(); 来注册窗口右上角关闭按钮动作[即先判断IFrame中表单内容是否变化]）
            //PageContext.RegisterPageStateChangedStartupScript(); // 注意这个语句应该放在if的外面，否则回发时会出错
            //btnClose.OnClientClick = PageContext.GetPageStateChangedFunction();

            // 4.第四种调用方法（推荐的做法，页面中的PageManager控件增加属性RegisterPageStateChangedScript="true"，并且在父页面中可以简单的 window1.OnClientCloseButtonClick = window1.GetIFramePageStateChangedFunction(); 来注册窗口右上角关闭按钮动作[即先判断IFrame中表单内容是否变化]）
            //btnClose.OnClientClick = PageContext.GetConfirmFormModifiedReference(); 
            #endregion
        }


        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Menu tm = new Menu();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                tm.MenuID = int.Parse(Request["id"]);
                tm.Attach();
                tm.MenuName = txt_ModeuleName.Text;
                tm.IsShow = txt_isuser.Checked;
                tm.WebUrl = txt_FunctionPage.Text;
                tm.IconUrl = txt_Icon.Text;
                tm.ParentID = int.Parse(txt_topmid.SelectedValue);
                if (txt_topmid.SelectedValue == "0")
                {
                    tm.MenuNode = 0;
                    tm.Node = "父节点";
                }
                else
                {
                    tm.MenuNode = 1;
                    tm.Node = "子节点";
                }
                returnvalue =SDDbSession.Default.Update<Menu>(tm);
            }
            else
            {
                tm.DeAttach();
                tm.MenuName = txt_ModeuleName.Text;
                tm.IsShow = txt_isuser.Checked;
                tm.WebUrl = txt_FunctionPage.Text;
                tm.IconUrl = txt_Icon.Text;
                tm.ParentID = int.Parse(txt_topmid.SelectedValue);
                if (txt_topmid.SelectedValue == "0")
                {
                    tm.MenuNode = 0;
                    tm.Node = "父节点";
                }
                else
                {
                    tm.MenuNode = 1;
                    tm.Node = "子节点";
                }
                returnvalue = SDDbSession.Default.Insert<Menu>(tm);
            }
            if (returnvalue > 0)
            {
               FineUI. PageContext.RegisterStartupScript(FineUI. ActiveWindow.GetHideRefreshReference());
            }
        }


        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    Alert.Show("提示内容");
        //}
    }
}